package org.example.traing.controller;


import org.example.traing.entity.ChatMessage;
import org.example.traing.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  // 标明这是一个REST控制器
public class ChatController {


    @Autowired
    ChatService chatService;


    // 获取聊天记录
    @GetMapping("/chat/messages/{roomId}")
    public List<ChatMessage> getMessages(@PathVariable String roomId) {
        return chatService.getMessages(roomId);
    }

    // 发送消息
    @PostMapping("/chat/send")
    public String sendMessage(@RequestParam String roomId, @RequestParam String senderId, @RequestParam String message) {
        ChatMessage chatMessage = new ChatMessage(roomId, senderId, message);
        System.out.println("************");
        chatService.saveMessage(chatMessage);
        return "Message sent";
    }
}

